//
//  JiGouViewController.m
//  JiuKuGospel
//
//  Created by jiukuyinyue on 16/8/23.
//  Copyright (c) 2016年 JiuKuForApp. All rights reserved.
//

#import "JiGouViewController.h"
#import "ShiGeCustomTableViewCell.h"
#import "JiangYuanViewController.h"
@interface JiGouViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView * jiangDaoTab;
@property (nonatomic, assign)NSInteger pageNum;

@property (nonatomic, strong)NSMutableArray * jiangYuanArr;

//@property (nonatomic, strong)MBProgressHUD *hud;
@end

@implementation JiGouViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.playSongView.hidden = NO;
    [appDelegate.window bringSubviewToFront:appDelegate.playSongView];
    [TheHomeManager shareTheHomeManager].nowViewController = self;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.hidden = NO;
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    //导航栏颜色
    self.navigationController.navigationBar.barTintColor = JK_HUISE_LINE_COLOR;
    //导航栏字体颜色
    self.navigationItem.title = self.titleStr;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    self.pageNum = 1;
    
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    UIImageView * LeftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 11, 19)];
    LeftImageView.userInteractionEnabled = YES;
    NSString * backStr = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"new返回%@", [TheHomeManager shareTheHomeManager].iPhone23] ofType:@"png"];
    LeftImageView.image = [UIImage imageWithContentsOfFile:backStr];
    [leftView addSubview:LeftImageView];
    UITapGestureRecognizer * backTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftBtnAction)];
    [leftView addGestureRecognizer:backTap];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];
    
    [self setupUI];
    [self loadJiangDaoData];
    
}


- (void)setupUI
{
    self.jiangDaoTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-60) style:UITableViewStylePlain];
    _jiangDaoTab.delegate = self;
    _jiangDaoTab.dataSource = self;
    [self.view addSubview:_jiangDaoTab];
    
    
    //cell分割线顶头
    if ([self.jiangDaoTab respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.jiangDaoTab setSeparatorInset: UIEdgeInsetsZero];
    }
    if ([self.jiangDaoTab respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.jiangDaoTab setLayoutMargins: UIEdgeInsetsZero];
    }
    [self setExtraCellLineHidden:_jiangDaoTab];
    
    __block JiGouViewController * jiangDVC = self;
    [_jiangDaoTab addHeaderWithCallback:^{
        jiangDVC.pageNum = 1;
        [jiangDVC loadJiangDaoData];
        
    }];
    
    [_jiangDaoTab addFooterWithCallback:^{
        jiangDVC.pageNum++;
        [jiangDVC loadJiangDaoData];
        
    }];

}

- (void)loadJiangDaoData
{
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode=MBProgressHUDAnimationFade;//枚举类型不同的效果
    NSLog(@"%@ %@ %@ ", self.typeStr , self.optStr, self.parentId);
    NSDictionary * dic = @{@"type":self.typeStr, @"opt":self.optStr, @"parentId":self.parentId, @"page":[NSString stringWithFormat:@"%ld", _pageNum], @"size":COUNT};

    [HttpTool postWithURL:kBasePostFuyin params:dic success:^(id json) {
        NSLog(@"%@", [json objectForKey:@"data"]);

        [self hiddenMBAction];
        if (_pageNum == 1) {
            [self.jiangYuanArr removeAllObjects];
        }
        JKSongsMessResult * results = [[JKSongsMessResult alloc] init];
        results.data = [json valueForKey:@"data"];
        NSArray * arr = [JKSongsMessStatus objectArrayWithKeyValuesArray:results.data];
        [self.jiangYuanArr addObjectsFromArray:arr];
        [self.jiangDaoTab reloadData];
        
        [_jiangDaoTab headerEndRefreshing];
        [_jiangDaoTab footerEndRefreshing];

        
        
    } failure:^(NSError *error) {
        [_jiangDaoTab headerEndRefreshing];
        [_jiangDaoTab footerEndRefreshing];
        [self hiddenMBAction];
    }];
}

- (void)leftBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

- (void)hiddenMBAction
{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 讲道Tab
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.jiangYuanArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    if (self.segmentMusicType.selectedSegmentIndex == 0) {      //热门讲道
    static NSString * cellStr = @"shigeCustomCell";
    ShiGeCustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [[ShiGeCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        cell.width = self.view.width;
        cell.height = 60;
    }
    
    JKSongsMessStatus * status = [self.jiangYuanArr objectAtIndex:indexPath.row];
    NSLog(@"%@", status.MusicName);
    cell.status = status;
    cell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];
    cell.zjNameLabel.text = [NSString stringWithFormat:@"%@篇讲道", status.total_music];
    
    //    cell.zjNameLabel.text = [NSString stringWithFormat:@"%@个专辑", status.total_album];
    cell.downLoadView.hidden = YES;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    JKSongsMessStatus * status = [self.jiangYuanArr objectAtIndex:indexPath.row];
    JiangYuanViewController * jiangYuanVC = [[JiangYuanViewController alloc] init];
    jiangYuanVC.titleStr = status.MusicName;
    jiangYuanVC.typeStr = self.typeStr;
    jiangYuanVC.optStr = @"3";
    jiangYuanVC.parentId = status.songID;
    [self.navigationController pushViewController:jiangYuanVC animated:YES];
    
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - 懒加载
- (NSMutableArray *)jiangYuanArr
{
    if (_jiangYuanArr == nil) {
        self.jiangYuanArr = [NSMutableArray array];
    }
    return _jiangYuanArr;
}


@end
